// 这里面存放vuex核心代码
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import setting from './modules/setting'

// 1.插件安装  2.创建仓库
Vue.use(Vuex)

const store = new Vuex.Store({
  strict: true,
  mutations: {
    subCount (state, obj) {
      console.log(obj)
      state.count -= obj.count
    },
    addCount (state, n) {
      state.count += n
    },
    changeCount (state, newCount) {
      state.count = newCount
    },
    changeTitle (state, newTitle) {
      state.title = newTitle
    }
  },
  // 3.actions异步处理 注：不能直接操作 state,操作state,还是需要commit mutation
  actions: {
    changeCountAsync (context, num) {
      setTimeout(() => {
        context.commit('changeCount', num)
      }, 1000)
    }
  },
  getters: {
    filterList (state) {
      return state.list.filter(item => item > 5)
    }
  },
  modules: {
    user,
    setting
  },
  state: {
    title: '仓库大标题',
    count: 100,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  }
})

// 导出给main.js使用
export default store
